package com.jy.rhin.valid;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 姚俊
 * @Date 2023/2/28 23:23
 * @Description 验证器工厂
 */
@Component
@Slf4j
public class ValidFactory {

    static Map<Class<? extends AbsValid>, AbsValid> validMap;
    static MiscValid miscValid;
    @Resource
    ApplicationContext applicationContext;

    @PostConstruct
    public void init(){
        validMap = new HashMap<>();
        for (Map.Entry<String, AbsValid> entry : applicationContext.getBeansOfType(AbsValid.class).entrySet()) {
            validMap.put(entry.getValue().getClass(), entry.getValue());
        }
        miscValid = applicationContext.getBean(MiscValid.class);
    }

     /**
       * @Description 获取验证器
       */
    public static AbsValid get(Class<? extends AbsValid> cls){
        return validMap.get(cls);
    }

    public static MiscValid getMiscValid(){
        return miscValid;
    }

}
